"""
 -*- coding: utf-8-*-
 @File: page_base.py
 @Author: chen
 @time: 2024/01/06/ 8:57
"""
from selenium import webdriver
from selenium.webdriver import ActionChains, Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait

from ui_test.page_po_test.page_utils.mgt_exception import record_exception
from ui_test.page_po_test.page_utils.mgt_logger import logger


class BasePage:
    def __init__(self, base_driver=None):
        if base_driver:
            self.driver = base_driver
        else:
            self.driver = webdriver.Chrome()
        self.by = By
        self.driver.maximize_window()
        self.driver.implicitly_wait(5)
        self.action = ActionChains(self.driver)
        self.logger = logger

    @record_exception
    def do_get(self, url):
        self.driver.get(url)
        self.logger.info(f"打开网站：{url}--->成功")

    def do_finds(self, locator: tuple):
        ele_s = self.driver.find_elements(*locator)
        return ele_s

    def do_find(self, locator: tuple, i=0):
        """获取单个元素"""
        ele = self.driver.find_elements(*locator)[i]
        logger.info(f"查找元素{locator}--->成功")
        return ele

    @record_exception
    def do_click(self, locator: tuple, i=0):
        """点击操作"""
        ele = self.wait_until_clickable(locator, i)
        ele.click()
        logger.info(f"点击元素{locator}--->成功")

    @record_exception
    def do_input(self, locator: tuple, content, i=0):
        """输入操作,先清空输入框，在输入内容"""
        ele = self.wait_until_visibility(locator, i)
        ele.send_keys(Keys.CONTROL, "A")
        logger.info("选中输入框--->成功")
        ele.send_keys(Keys.DELETE)
        logger.info("清空输入框--->成功")
        ele.send_keys(content)
        logger.info(f"元素{locator}输入：{content}--->成功")

    @record_exception
    def do_quit(self):
        self.driver.quit()
        logger.info("退出浏览器--->成功")

    def wait_until_visibility(self, locator: tuple, i=0):
        obj = WebDriverWait(self.driver, 10).until(
            expected_conditions.visibility_of_element_located(locator)
        )
        if obj:
            logger.info(f"元素{locator}--->可见")
            return self.do_find(locator, i)
        else:
            logger.info(f"元素{locator}--->不可见")
            return None

    def wait_until_presence(self, locator: tuple, i=0):
        obj = WebDriverWait(self.driver, 10).until(
            expected_conditions.presence_of_element_located(locator)
        )
        if obj:
            logger.info(f"元素{locator}--->可见")
            return self.do_find(locator, i)
        else:
            logger.info(f"元素{locator}--->不可见")
            return None

    def wait_until_clickable(self, locator: tuple, i=0):
        obj = WebDriverWait(self.driver, 10).until(
            expected_conditions.element_to_be_clickable(locator)
        )
        if obj:
            logger.info(f"等待元素{locator}可点击--->成功")
            return self.do_find(locator, i)
        else:
            logger.info(f"等待元素{locator}可点击--->失败")
            return None
